<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class UserController extends Controller
{
    /**
     * 显示用户个人信息
     */
    public function profile(): Response
    {
        return Inertia::render('Profile/Index');
    }

    /**
     * 更新用户个人信息
     */
    public function updateProfile(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'birthday' => ['nullable', 'date'],
            'bio' => ['nullable', 'string', 'max:500'],
        ]);

        $request->user()->update($validated);

        return back()->with('success', '个人信息更新成功');
    }

    /**
     * 显示账户设置
     */
    public function settings(): Response
    {
        $user = auth()->user();
        
        return Inertia::render('Profile/Settings', [
            'userSettings' => [
                'order_notifications' => $user->order_notifications ?? true,
                'promo_notifications' => $user->promo_notifications ?? false,
                'data_collection' => $user->data_collection ?? true,
            ]
        ]);
    }

    /**
     * 更新账户设置
     */
    public function updateSettings(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'order_notifications' => ['boolean'],
            'promo_notifications' => ['boolean'],
            'data_collection' => ['boolean'],
        ]);

        $request->user()->update($validated);

        return back()->with('success', '设置已保存');
    }
}
